using System;
using System.Collections.Generic;
using System.Text;
using FlatRedBall.AI.Pathfinding;
using FlatRedBall.Graphics.Model;

using FlatRedBall.Input;
using FlatRedBall.Utilities;

using FlatRedBall.Instructions;
using FlatRedBall.Math.Splines;
using BitmapFont = FlatRedBall.Graphics.BitmapFont;
using Cursor = FlatRedBall.Gui.Cursor;
using GuiManager = FlatRedBall.Gui.GuiManager;
// Generated Usings
using NereusGame.Screens;
using Matrix = Microsoft.Xna.Framework.Matrix;
using FlatRedBall.Graphics;
using FlatRedBall.Math;
using FlatRedBall.Broadcasting;
using NereusGame.Entities;
using NereusGame.Entities.HBodies;
using NereusGame.Entities.Projectiles;
using NereusGame.Entities.Ships;
using NereusGame.Entities.Ships.Enemies;
using NereusGame.Entities.Weapons;
using FlatRedBall;
using FlatRedBall.Math.Geometry;
using Microsoft.Xna.Framework.Graphics;

#if XNA4
using Color = Microsoft.Xna.Framework.Color;
#else
using Color = Microsoft.Xna.Framework.Graphics.Color;
#endif

#if FRB_XNA || SILVERLIGHT
using Keys = Microsoft.Xna.Framework.Input.Keys;
using Vector3 = Microsoft.Xna.Framework.Vector3;
using Texture2D = Microsoft.Xna.Framework.Graphics.Texture2D;
#endif

#if FRB_XNA && !MONODROID
using Model = Microsoft.Xna.Framework.Graphics.Model;
#endif

namespace NereusGame.Entities.HBodies
{
	public partial class Star : NereusGame.Entities.HBodies.HBody, IDestroyable
	{
        // This is made global so that static lazy-loaded content can access it.
        public static new string ContentManagerName
        {
            get{ return Entities.HBodies.HBody.ContentManagerName;}
            set{ Entities.HBodies.HBody.ContentManagerName = value;}
        }

		// Generated Fields
		#if DEBUG
		static bool HasBeenLoadedWithGlobalContentManager = false;
		#endif
		static object mLockObject = new object();
		static bool mHasRegisteredUnload = false;
		static bool IsStaticContentLoaded = false;
		private static Texture2D OrangeStar;
		private static Texture2D OrangeStarRays;
		
		private FlatRedBall.Sprite mSunRays;
		public FlatRedBall.Sprite SunRays
		{
			get
			{
				return mSunRays;
			}
		}
		private FlatRedBall.Math.Geometry.Circle MaxRing;
		public int Index { get; set; }
		public bool Used { get; set; }

        public Star(string contentManagerName) :
            this(contentManagerName, true)
        {
        }


        public Star(string contentManagerName, bool addToManagers) :
			base(contentManagerName, addToManagers)
		{
			// Don't delete this:
            ContentManagerName = contentManagerName;
           

		}

		protected override void InitializeEntity(bool addToManagers)
		{
			// Generated Initialize
			LoadStaticContent(ContentManagerName);
			mSunRays = new FlatRedBall.Sprite();
			MaxRing = new FlatRedBall.Math.Geometry.Circle();
			
			base.InitializeEntity(addToManagers);


		}

// Generated AddToManagers
		public override void AddToManagers (Layer layerToAddTo)
		{
			LayerProvidedByContainer = layerToAddTo;
			base.AddToManagers(layerToAddTo);
			CustomInitialize();
		}

		public override void Activity()
		{
			// Generated Activity
			base.Activity();
			
			CustomActivity();
			
			// After Custom Activity
		}

		public override void Destroy()
		{
			// Generated Destroy
			base.Destroy();
			
			if (SunRays != null)
			{
				SunRays.Detach(); SpriteManager.RemoveSprite(SunRays);
			}
			if (MaxRing != null)
			{
				MaxRing.Detach(); ShapeManager.Remove(MaxRing);
			}


			CustomDestroy();
		}

		// Generated Methods
		public override void PostInitialize ()
		{
			base.PostInitialize();
			if (mBody!= null && mBody.Parent == null)
			{
				mBody.CopyAbsoluteToRelative();
				mBody.AttachTo(this, false);
			}
			Body.Visible = false;
			if (mGravRing!= null && mGravRing.Parent == null)
			{
				mGravRing.CopyAbsoluteToRelative();
				mGravRing.AttachTo(this, false);
			}
			if (mTexture!= null && mTexture.Parent == null)
			{
				mTexture.CopyAbsoluteToRelative();
				mTexture.AttachTo(this, false);
			}
			if (mSunRays!= null && mSunRays.Parent == null)
			{
				mSunRays.CopyAbsoluteToRelative();
				mSunRays.AttachTo(this, false);
			}
			if (mBody!= null && mBody.Parent == null)
			{
				mBody.CopyAbsoluteToRelative();
				mBody.AttachTo(this, false);
			}
			if (mGravRing!= null && mGravRing.Parent == null)
			{
				mGravRing.CopyAbsoluteToRelative();
				mGravRing.AttachTo(this, false);
			}
			if (mTexture!= null && mTexture.Parent == null)
			{
				mTexture.CopyAbsoluteToRelative();
				mTexture.AttachTo(this, false);
			}
			if (MaxRing!= null && MaxRing.Parent == null)
			{
				MaxRing.CopyAbsoluteToRelative();
				MaxRing.AttachTo(this, false);
			}
		}
		public override void AddToManagersBottomUp (Layer layerToAddTo)
		{
			base.AddToManagersBottomUp(layerToAddTo);
			// We move this back to the origin and unrotate it so that anything attached to it can just use its absolute position
			float oldRotationX = RotationX;
			float oldRotationY = RotationY;
			float oldRotationZ = RotationZ;
			
			float oldX = X;
			float oldY = Y;
			float oldZ = Z;
			
			X = 0;
			Y = 0;
			Z = 0;
			RotationX = 0;
			RotationY = 0;
			RotationZ = 0;
			mBody.Visible = false;
			SpriteManager.AddToLayer(mSunRays, layerToAddTo);
			ShapeManager.AddToLayer(MaxRing, layerToAddTo);
			X = oldX;
			Y = oldY;
			Z = oldZ;
			RotationX = oldRotationX;
			RotationY = oldRotationY;
			RotationZ = oldRotationZ;
		}
		public override void ConvertToManuallyUpdated ()
		{
			base.ConvertToManuallyUpdated();
			this.ForceUpdateDependenciesDeep();
			SpriteManager.ConvertToManuallyUpdated(this);
			SpriteManager.ConvertToManuallyUpdated(SunRays);
		}
		public static new void LoadStaticContent (string contentManagerName)
		{
			ContentManagerName = contentManagerName;
			#if DEBUG
			if (contentManagerName == FlatRedBallServices.GlobalContentManager)
			{
				HasBeenLoadedWithGlobalContentManager = true;
			}
			else if (HasBeenLoadedWithGlobalContentManager)
			{
				throw new Exception("This type has been loaded with a Global content manager, then loaded with a non-global.  This can lead to a lot of bugs");
			}
			#endif
			if (IsStaticContentLoaded == false)
			{
				IsStaticContentLoaded = true;
				lock (mLockObject)
				{
					if (!mHasRegisteredUnload && ContentManagerName != FlatRedBallServices.GlobalContentManager)
					{
						FlatRedBallServices.GetContentManagerByName(ContentManagerName).AddUnloadMethod("StarStaticUnload", UnloadStaticContent);
						mHasRegisteredUnload = true;
					}
				}
				bool registerUnload = false;
				if (!FlatRedBallServices.IsLoaded<Texture2D>(@"content/entities/star/orangestar.png", ContentManagerName))
				{
					registerUnload = true;
				}
				OrangeStar = FlatRedBallServices.Load<Texture2D>(@"content/entities/star/orangestar.png", ContentManagerName);
				if (!FlatRedBallServices.IsLoaded<Texture2D>(@"content/entities/star/orangestarrays.png", ContentManagerName))
				{
					registerUnload = true;
				}
				OrangeStarRays = FlatRedBallServices.Load<Texture2D>(@"content/entities/star/orangestarrays.png", ContentManagerName);
				if (registerUnload && ContentManagerName != FlatRedBallServices.GlobalContentManager)
				{
					lock (mLockObject)
					{
						if (!mHasRegisteredUnload && ContentManagerName != FlatRedBallServices.GlobalContentManager)
						{
							FlatRedBallServices.GetContentManagerByName(ContentManagerName).AddUnloadMethod("StarStaticUnload", UnloadStaticContent);
							mHasRegisteredUnload = true;
						}
					}
				}
				CustomLoadStaticContent(contentManagerName);
			}
		}
		public static new void UnloadStaticContent ()
		{
			IsStaticContentLoaded = false;
			mHasRegisteredUnload = false;
			if (OrangeStar != null)
			{
				OrangeStar= null;
			}
			if (OrangeStarRays != null)
			{
				OrangeStarRays= null;
			}
		}
		public static new object GetStaticMember (string memberName)
		{
			switch(memberName)
			{
				case  "OrangeStar":
					return OrangeStar;
				case  "OrangeStarRays":
					return OrangeStarRays;
			}
			return null;
		}
		object GetMember (string memberName)
		{
			switch(memberName)
			{
				case  "OrangeStar":
					return OrangeStar;
				case  "OrangeStarRays":
					return OrangeStarRays;
			}
			return null;
		}
		public override void SetToIgnorePausing ()
		{
			base.SetToIgnorePausing();
			InstructionManager.IgnorePausingFor(Body);
			InstructionManager.IgnorePausingFor(GravRing);
			InstructionManager.IgnorePausingFor(Texture);
			InstructionManager.IgnorePausingFor(SunRays);
			InstructionManager.IgnorePausingFor(Body);
			InstructionManager.IgnorePausingFor(GravRing);
			InstructionManager.IgnorePausingFor(Texture);
			InstructionManager.IgnorePausingFor(MaxRing);
		}

    }
	
	
	// Extra classes
	public static class StarExtensionMethods
	{
	}
	
}
